﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeiDuan.Model;
using WeiDuan.Service;
using B2B.Service;
using B2B.Business;
using B2B.Business.Impl;
using Autofac;

namespace B2B.Service.Impl
{
    [WeiDuanService(Name = "B2B.OrderService", Description = "订单管理")]
     public class OrderService : BaseService, IOrderService, IAppService
    {
        private IOrderBusiness mBusiness = null;
        public OrderService()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<B2B.Business.Impl.OrderBusiness>().As<IOrderBusiness>();
            using (var container = builder.Build())
            {
                mBusiness = container.Resolve<IOrderBusiness>();
            }
            base.eventCreate += OrderService_eventCreate;
        }

        public void OrderService_eventCreate(IWeiDuanService service)
        {
            if (mBusiness is IServiceInjection)
                ((IServiceInjection)mBusiness).Injection(mWeiDuanService);
        }

        
        [WeiDuanInterface(Name = "QueryProductPrice", Description = "根据类别查询商品")]
        public List<ProductPackSKUInfo> QueryProductPrice(string ClassID, UserInfo user)
        {
            return mBusiness.QueryProductPrice(ClassID, user);
        }

        [WeiDuanInterface(Name = "Add", Description = "添加订单")]
        public string Add(OrderAddRequest req, UserInfo user)
        {
            return mBusiness.Add(req, user);
        }

        [WeiDuanInterface(Name = "Update", Description = "修改订单")]
        public void Update(OrderUpdateRequest req, UserInfo user)
        {
            mBusiness.Update(req, user);
        }

        [WeiDuanInterface(Name = "Submit", Description = "提交订单")]
        public void Submit(string OrderID, UserInfo user)
        {
            mBusiness.Submit(OrderID, user);
            mBusiness.Audit(OrderID, user);
        }

        [WeiDuanInterface(Name = "Delete", Description = "作废订单")]
        public void Delete(string id, UserInfo user)
        {
            mBusiness.Delete(id, user);
        }

        [WeiDuanInterface(Name = "DeleteItem", Description = "删除订单中的一笔商品")]
        public void DeleteItem(string itemId, UserInfo user)
        {
            mBusiness.DeleteItem(itemId, user);
        }

       
         [WeiDuanInterface(Name = "Audit", Description = "销售方确认订单")]
        public void Audit(string id, UserInfo user)
        {
            mBusiness.Audit(id, user);
        }
 
        [WeiDuanInterface(Name = "ProviderConfirm", Description = "供应商确认")]
         public void ProviderConfirm(string id, UserInfo user)
        {
            mBusiness.ProviderConfirm(id, user);
        }
        
        [WeiDuanInterface(Name = "SendExpress", Description = "销售方发货")]
        public void SendExpress(SendExpressRequest req, UserInfo user)
        {
            mBusiness.SendExpress(req, user);
        }

   
        [WeiDuanInterface(Name = "AcceptExpress", Description = "购买方确认收货")]
        public void AcceptExpress(string ExpressID, UserInfo user)
        {
            mBusiness.AcceptExpress(ExpressID, user);
        }

        
        [WeiDuanInterface(Name = "Query", Description = "根据订单状态查询订单清单（销售方查询）")]
        public List<OrderInfo> Query(OrderQueryRequest req, UserInfo user)
        {
            return mBusiness.Query(req, user);
        }


        [WeiDuanInterface(Name = "QueryBySaler", Description = "根据订单状态查询订单清单(平台查询)")]
        public List<OrderInfo> QueryBySaler(OrderQueryRequest req, UserInfo user)
        {
            return mBusiness.QueryBySaler(req, user);
        }
         
        [WeiDuanInterface(Name = "QueryByProvider", Description = "根据订单状态查询订单清单(供货商查询)")]
        public List<OrderInfo> QueryByProvider(OrderQueryRequest req, UserInfo user)
        {
            return mBusiness.QueryByProvider(req, user);
        }

      
        [WeiDuanInterface(Name = "QueryDetail", Description = "查询单笔订单信息")]
        public OrderInfo QueryDetail(string id, UserInfo user)
        {
            return mBusiness.QueryDetail(id, user);
        }

        [WeiDuanInterface(Name = "GetOrderStatus", Description = "查询订单的状态")]
        public EnumValue[] GetOrderStatus(UserInfo user)
        {
            return WeiDuan.Model.EnumHelper.EnumToArray(typeof(OrderStatusEnum), o => o.GetDescription());
        }

        [WeiDuanInterface(Name = "GetOrderStatusByProvider", Description = "查询订单的状态，仅包括供应商所应看到的状态")]
        public EnumValue[] GetOrderStatusByProvider(UserInfo user)
        {
            List<EnumValue> ret = new List<EnumValue>();
            //ret.Add(new EnumValue("等待确认", (int)OrderStatusEnum.Audited));
            //ret.Add(new EnumValue("已确认，等待发货", (int)OrderStatusEnum.ProviderConfirm));
            ret.Add(new EnumValue(OrderStatusEnum.Audited));//"等待确认
            ret.Add(new EnumValue(OrderStatusEnum.ProviderConfirm));//已确认，等待发货
            ret.Add(new EnumValue(OrderStatusEnum.SendExpress));
            ret.Add(new EnumValue(OrderStatusEnum.AcceptExpress));
            ret.Add(new EnumValue(OrderStatusEnum.Charge));
            ret.Add(new EnumValue(OrderStatusEnum.Payment));
         
            return ret.ToArray();
        }

        [WeiDuanInterface(Name = "QueryPage", Description = "分页查询订单")]
        public OrderQueryResponse QueryPage(PageQueryRequest<OrderQueryRequest> req, UserInfo user)
        {
            return mBusiness.QueryPage(req, user);
        }

        [WeiDuanInterface(Name = "QueryByProviderPage", Description = "分布查询订单(供货商查询)")]
        public OrderQueryResponse QueryByProviderPage(PageQueryRequest<OrderQueryRequest> req, UserInfo user)
        {
            return mBusiness.QueryByProviderPage(req, user);
        }

        [WeiDuanInterface(Name = "ProviderCancel", Description = "供应商退回客户下所的采购订单")]
        public void ProviderCancel(string id, UserInfo user)
        {
            mBusiness.ProviderCancel(id, user);
        }
    }
}
